package com.xm9m.web.service.security;

import com.salesmanager.core.business.user.service.UserService;
import com.xm9m.web.service.entity.security.SecurityCustomer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;

public class CustomerAuthenticationSuccessHandler extends
		SavedRequestAwareAuthenticationSuccessHandler {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(CustomerAuthenticationSuccessHandler.class);
	
	@Autowired
	private UserService userService;
	
	  @Override
	    public void onAuthenticationSuccess(HttpServletRequest request2, HttpServletResponse response, Authentication authentication) {
		  // last access timestamp
		  HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		  Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		  if (!(auth instanceof AnonymousAuthenticationToken)) {
			  SecurityCustomer userDetails =
					  (SecurityCustomer)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		  }
		  Object securityCustomer=authentication.getDetails();
		  String userName = authentication.getName();
		  String tokenUrl="/oauth.token.get/"+"1.0";//
		try {
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			out.println("<form name='paySubmit' method='post'  action='" + tokenUrl + "' >");

			Map properties = request.getParameterMap();
			Iterator entries = properties.entrySet().iterator();
			Map.Entry entry;
			String name = "";
			String value = "";
			StringBuilder sb = new StringBuilder();
			while (entries.hasNext()) {
				entry = (Map.Entry) entries.next();
				name = (String) entry.getKey();
				Object valueObj = entry.getValue();
				sb.append(name);
				sb.append("=");

				if (null == valueObj) {
					sb.append(value);
				} else if (valueObj instanceof String[]) {
					String[] values = (String[]) valueObj;
					for (int i = 0; i < values.length; i++) {
						value = values[i] + ",";
					}
					value = value.substring(0, value.length() - 1);
				} else {
					value = valueObj.toString();
				}
				sb.append(value);
				sb.append("&");
				out.println("<input type='hidden' name='" + name + "' value='" + value + "'>");
			}

			out.println("</form>");
			out.println("<script>");
			out.println("  document.paySubmit.submit()");
			out.println("</script>");
			out.flush();
			out.close();
		}catch (Exception ee){
			logger.error(ee.getMessage(),ee);
		}
	  }
}
